package org.ddr.prezi;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ddr.service.PreziService;
import org.ddr.vo.TestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	@Inject
	PreziService service;

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home() throws Exception {
		List<TestVO> list = service.preziTestOne();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("preziTest");
		mav.addObject("preziList", list);
		return mav;
	}

	@RequestMapping(value = "edit")
	public ModelAndView editPrezi(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<TestVO> list = service.preziTestOne();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("edittest");
		mav.addObject("preziList", list);
		return mav;
	}
	
	@RequestMapping("test")
	public ModelAndView testPrezi() throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("canvas");
		return mav;
	}

	@RequestMapping(value = "resources/makeshape")
	public ModelAndView makeShape(HttpServletRequest request, HttpServletResponse response) throws Exception {

		response.setContentType("image/jpg");
		BufferedImage img = new BufferedImage(120, 80, BufferedImage.TYPE_INT_BGR);
		OutputStream out = response.getOutputStream();
		Graphics g = img.getGraphics();
		g.setColor(Color.ORANGE);
		g.fillRect(0, 0, 120, 80);
		g.setColor(Color.BLACK);

		Font f = new Font("Arial", Font.CENTER_BASELINE, 9);
		g.setFont(f);

		List<TestVO> list = service.preziTestOne();
		ModelAndView mav = new ModelAndView();

		String str = "<p>Its <strong>presentation tool</strong></br>inspired by theidea behide <a href='http://prezi.com'>prezi.com</a></br>" +
				"and base on the <strong>power of css3...</strong>in modern browers.</p>?";

		HttpSession session = request.getSession();
		session.setAttribute("str", str);
		g.drawString(str, 0, 30);
		ImageIO.write(img, "jpg", out);
		return null;
	}
	
	@RequestMapping(value="editor")
	public ModelAndView editor() throws Exception{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/editor/SEditorDemo");
		return mav;
	}
	
	@RequestMapping(value="skin")
	public ModelAndView skin() throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/editor/SEditorSkin");
		return mav;
	}
	
	@RequestMapping(value="se_blank")
	public ModelAndView se_blank() throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/editor/se_blank");
		return mav;
	}

}
